package com.example.swing.jframe2;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;


public class JFrameTextArea extends JFrame {
    private JButton
            button1 = new JButton("add data"),
            button2 = new JButton("clear data");
    private JTextArea txt = new JTextArea(10, 20);
    private Map<String, String> map = new HashMap<>();
    Button1Listener b1 = new Button1Listener();
    Button2Listener b2 = new Button2Listener();
    public JFrameTextArea() {
        //map.putAll(Countries.capitals(10));
        button1.addActionListener(b1);
        button2.addActionListener(b2);
        setLayout(new FlowLayout());
        add(button1);
        add(button2);
        add(txt);
    }
    class Button1Listener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String name = ((JButton) e.getSource()).getText();
            txt.setText(name);
            for(Map.Entry m : map.entrySet()){
                txt.append(m.getKey() + ": " + m.getValue() + "\n");
            }
        }
    }
    class Button2Listener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String name = ((JButton) e.getSource()).getText();
            txt.setText("");
        }
    }



    public static void main(String[] args) {
        //run(new JFrameTextArea(), 200, 150);
    }
}
